package com.senmo.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.senmo.base.ActionBar;
import com.senmo.base.BaseActivity;
import com.senmo.data.DeviceDataProvider;
import com.senmo.model.ChannelModel;
import com.senmo.model.DeviceModel;
import com.senmo.senmo.R;

import java.util.ArrayList;

import lx.af.adapter.AbsViewHolder;
import lx.af.adapter.AbsViewHolderAdapter;
import lx.af.utils.ViewInject.ViewInject;

/**
 * author: lx
 * date: 16-1-6
 */
public class ChannelListActivity extends BaseActivity implements
        DeviceDataProvider.DeviceChannelCallback,
        AdapterView.OnItemClickListener,
        ActionBar.Default {

    private ListView mListView;
    private ChannelAdapter mAdapter;
    private DeviceModel mDevice;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String device_id = getIntent().getStringExtra("device_id");
        mDevice = DeviceDataProvider.getInstance().getDevice(device_id);
        if (mDevice == null) {
            toastShort(R.string.channel_list_toast_no_id);
            finish();
            return;
        }

        setContentView(R.layout.activity_channel_list);
        mAdapter = new ChannelAdapter(this);
        mListView = obtainView(R.id.activity_channel_list_list);
        mListView.addHeaderView(new ViewStub(this));
        mListView.addFooterView(new ViewStub(this));
        mListView.setOnItemClickListener(this);
        mListView.setAdapter(mAdapter);

        showLoadingDialog();
        DeviceDataProvider.getInstance().getDeviceChannelList(device_id, false, this);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        ChannelModel channel = mAdapter.getItem(i - 1);
        Intent intent = new Intent(this, ChannelVideoActivity.class);
        intent.putExtra("channel_video_url", channel.url);
        startActivity(intent);
    }

    @Override
    public void onGetDeviceChannels(ArrayList<ChannelModel> list) {
        dismissLoadingDialog();
        if (list != null && list.size() != 0) {
            mAdapter.addAll(list);
        }
    }

    private static class ChannelAdapter extends AbsViewHolderAdapter<ChannelModel> {

        public ChannelAdapter(Context context) {
            super(context);
        }

        @Override
        public View createItemView(Context context) {
            return View.inflate(context, R.layout.item_channel, null);
        }

        @Override
        public AbsViewHolder<ChannelModel> createViewHolder(View itemView) {
            return new ViewHolder(itemView);
        }

        private static class ViewHolder extends AbsViewHolder<ChannelModel> {

            @ViewInject(id = R.id.item_channel_name)
            TextView name;
            @ViewInject(id = R.id.item_channel_default_icon)
            TextView icon;
            @ViewInject(id = R.id.item_channel_preview)
            ImageView preview;

            public ViewHolder(View root) {
                super(root);
            }

            @Override
            public void setData(ChannelModel data) {
                name.setText(data.name);
            }
        }
    }

}
